/*
 Author:	david lee
 Date:		22 July 2007
 Class:		ToolBox
 e-mail:	david_623@sohu.com
 Version:	1.0 
*/

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace CUIT.GraduationProject.ToolBoxSystem.ToolBoxLib
{
	/// <summary>
	/// Summary description for UserControl1.
	/// </summary>
	public class ToolBox : System.Windows.Forms.UserControl
	{
		enum States	{Compressed=1,Compressing,Extended,Extending};

		private System.ComponentModel.IContainer components;
		private Button ScrollButton1 = new Button();
		private Button ScrollButton2 = new Button();
		private int ButtonHeight = 22;//18;
		private int XOffset = 22;
		private int YOffset = 20;
		private int RightMargin = 5;
		private int Acceleration = 2;
		private bool Bounce = true;
		private int ScrollButtonX;
		private int ButtonWidth;
		private int ListWidth;
		private int Delay = 2000;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Label Title;
		private System.Windows.Forms.Button CloseButton;
		private System.Windows.Forms.Button Pioneza;
		private System.Windows.Forms.ImageList ButtonImageList;
		private bool DoneMove = true;
		private System.Windows.Forms.ToolTip toolTip;
		private bool Retreat = true;
		private bool RetreatCommand = false;
		private bool ExpandCommand = false;
		private string Separator = ".";
		private int state = (int)States.Extended;
		private System.Windows.Forms.MenuItem Menu_Dock_Left;
		private System.Windows.Forms.MenuItem Menu_Dock_Right;
		private System.Windows.Forms.MenuItem Menu_Autohide;
		private System.Windows.Forms.MenuItem Menu_Hide;
		private System.Windows.Forms.MenuItem Menu_About;
		private ContextMenu CMenu = null;

		public delegate void OnToolBoxClick(int TabIndex,int ItemIndex);
		public delegate void OnToolBoxTabChanged(int TabIndex);
		public delegate void OnToolBoxStateChanged(int State);

		private OnToolBoxClick OnTBClick = null;
		private OnToolBoxTabChanged OnTBTabChanged=  null;
		private System.Windows.Forms.Panel panel;
		private System.Windows.Forms.PictureBox Picture;
		private System.Windows.Forms.ContextMenu contextMenu;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.Timer timer1;
		private OnToolBoxStateChanged OnTBStateChanged = null;

		public void  SetClickDelegate(OnToolBoxClick OnTBxClick)
		{
			OnTBClick = OnTBxClick;
		}
		
		public  void SetTabChangedDelegate(OnToolBoxTabChanged OnTBTabChange)
		{
			OnTBTabChanged = OnTBTabChange;
		}

		public  void SetStateChangedDelegate(OnToolBoxStateChanged OnTBStateChange)
		{
			OnTBStateChanged = OnTBStateChange;
		}

		private void List_Click(object sender, System.EventArgs e)
		{
			Point pt = VisibleLV.PointToClient(MousePosition);
			ListViewItem itm = VisibleLV.GetItemAt(pt.X,pt.Y);

			if(itm!=null && OnTBClick!=null)
			{
				itm.Selected = true;
				OnTBClick(SelectedTab,itm.Index);
			}
		}

		public ToolBox(Size sz, int Button_Height, ContextMenu Menu)
		{			
			InitializeComponent();
			this.Size = sz;

			CMenu = Menu;
			ButtonWidth = ClientRectangle.Width - XOffset-2-RightMargin;
			ButtonHeight = (Button_Height < 0) ? ButtonHeight : Button_Height;
			ListWidth = ButtonWidth + 100;
			ScrollButtonX = (ClientRectangle.Width - 19 -RightMargin);
			ScrollButton1.ImageAlign = System.Drawing.ContentAlignment.MiddleCenter;
			ScrollButton1.Location = new System.Drawing.Point(ScrollButtonX,YOffset);
			ScrollButton1.Size = new Size(17,ButtonHeight);
			ScrollButton1.Name = "button";
			ScrollButton1.Image = this.imageList1.Images[0];
			ScrollButton1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

			ScrollButton1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.SB1_MouseUp);
            ScrollButton1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SB1_MouseDown);

			this.Controls.Add(ScrollButton1);

			ScrollButton2.ImageAlign = System.Drawing.ContentAlignment.MiddleCenter;
			ScrollButton2.Location = new System.Drawing.Point(ScrollButtonX,YOffset);
			ScrollButton2.Size = new Size(17, ButtonHeight);
			ScrollButton2.Name = "button";
			ScrollButton2.Image = this.imageList1.Images[1];
			ScrollButton2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			ScrollButton2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.SB2_MouseUp);
			ScrollButton2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SB2_MouseDown);
			this.Controls.Add(ScrollButton2);
		}
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ToolBox));
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.Title = new System.Windows.Forms.Label();
            this.contextMenu = new System.Windows.Forms.ContextMenu();
            this.Menu_Dock_Left = new System.Windows.Forms.MenuItem();
            this.Menu_Dock_Right = new System.Windows.Forms.MenuItem();
            this.Menu_Autohide = new System.Windows.Forms.MenuItem();
            this.Menu_Hide = new System.Windows.Forms.MenuItem();
            this.Menu_About = new System.Windows.Forms.MenuItem();
            this.CloseButton = new System.Windows.Forms.Button();
            this.Pioneza = new System.Windows.Forms.Button();
            this.ButtonImageList = new System.Windows.Forms.ImageList(this.components);
            this.toolTip = new System.Windows.Forms.ToolTip(this.components);
            this.panel = new System.Windows.Forms.Panel();
            this.Picture = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.Picture)).BeginInit();
            this.SuspendLayout();
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.White;
            this.imageList1.Images.SetKeyName(0, "");
            this.imageList1.Images.SetKeyName(1, "");
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Interval = 20;
            this.timer1.Tick += new System.EventHandler(this.TimerTick);
            // 
            // splitter1
            // 
            this.splitter1.Dock = System.Windows.Forms.DockStyle.Right;
            this.splitter1.Location = new System.Drawing.Point(213, 0);
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(3, 456);
            this.splitter1.TabIndex = 1;
            this.splitter1.TabStop = false;
            this.splitter1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Splitter_MouseMove);
            this.splitter1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Splitter_MouseDown);
            this.splitter1.Paint += new System.Windows.Forms.PaintEventHandler(this.Splitter_Paint);
            // 
            // Title
            // 
            this.Title.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.Title.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.Title.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Title.ContextMenu = this.contextMenu;
            this.Title.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Title.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.Title.Location = new System.Drawing.Point(21, 2);
            this.Title.Name = "Title";
            this.Title.Size = new System.Drawing.Size(200, 17);
            this.Title.TabIndex = 3;
            this.Title.Text = "ToolBox";
            this.Title.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // contextMenu
            // 
            this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.Menu_Dock_Left,
            this.Menu_Dock_Right,
            this.Menu_Autohide,
            this.Menu_Hide,
            this.Menu_About});
            // 
            // Menu_Dock_Left
            // 
            this.Menu_Dock_Left.Index = 0;
            this.Menu_Dock_Left.Text = "Dock Left";
            this.Menu_Dock_Left.Visible = false;
            this.Menu_Dock_Left.Click += new System.EventHandler(this.Menu_Dock_Left_Click);
            // 
            // Menu_Dock_Right
            // 
            this.Menu_Dock_Right.Index = 1;
            this.Menu_Dock_Right.Text = "Dock Right";
            this.Menu_Dock_Right.Visible = false;
            this.Menu_Dock_Right.Click += new System.EventHandler(this.Menu_Dock_Right_Click);
            // 
            // Menu_Autohide
            // 
            this.Menu_Autohide.Checked = true;
            this.Menu_Autohide.Index = 2;
            this.Menu_Autohide.Text = "AutoHide";
            this.Menu_Autohide.Click += new System.EventHandler(this.Menu_Autohide_Click);
            // 
            // Menu_Hide
            // 
            this.Menu_Hide.Index = 3;
            this.Menu_Hide.Text = "Hide";
            this.Menu_Hide.Click += new System.EventHandler(this.Menu_Hide_Click);
            // 
            // Menu_About
            // 
            this.Menu_About.Index = 4;
            this.Menu_About.Text = "About";
            this.Menu_About.Click += new System.EventHandler(this.Menu_About_Click);
            // 
            // CloseButton
            // 
            this.CloseButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.CloseButton.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.CloseButton.Font = new System.Drawing.Font("Lucida Console", 8.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.CloseButton.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.CloseButton.Location = new System.Drawing.Point(194, 3);
            this.CloseButton.Name = "CloseButton";
            this.CloseButton.Size = new System.Drawing.Size(16, 15);
            this.CloseButton.TabIndex = 4;
            this.CloseButton.Text = "X";
            this.toolTip.SetToolTip(this.CloseButton, "Hide");
            this.CloseButton.UseVisualStyleBackColor = false;
            this.CloseButton.Click += new System.EventHandler(this.CloseButton_Click);
            // 
            // Pioneza
            // 
            this.Pioneza.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.Pioneza.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.Pioneza.Font = new System.Drawing.Font("Lucida Console", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Pioneza.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.Pioneza.ImageIndex = 0;
            this.Pioneza.ImageList = this.ButtonImageList;
            this.Pioneza.Location = new System.Drawing.Point(176, 3);
            this.Pioneza.Name = "Pioneza";
            this.Pioneza.Size = new System.Drawing.Size(16, 15);
            this.Pioneza.TabIndex = 5;
            this.toolTip.SetToolTip(this.Pioneza, "Auto Hide");
            this.Pioneza.UseVisualStyleBackColor = false;
            this.Pioneza.Click += new System.EventHandler(this.Pioneza_Click);
            // 
            // ButtonImageList
            // 
            this.ButtonImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ButtonImageList.ImageStream")));
            this.ButtonImageList.TransparentColor = System.Drawing.Color.Transparent;
            this.ButtonImageList.Images.SetKeyName(0, "");
            this.ButtonImageList.Images.SetKeyName(1, "");
            // 
            // panel
            // 
            this.panel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)));
            this.panel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(69)))), ((int)(((byte)(69)))), ((int)(((byte)(69)))));
            this.panel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel.Location = new System.Drawing.Point(0, 0);
            this.panel.Name = "panel";
            this.panel.Size = new System.Drawing.Size(21, 456);
            this.panel.TabIndex = 6;
            // 
            // Picture
            // 
            this.Picture.Image = ((System.Drawing.Image)(resources.GetObject("Picture.Image")));
            this.Picture.Location = new System.Drawing.Point(0, 0);
            this.Picture.Name = "Picture";
            this.Picture.Size = new System.Drawing.Size(20, 104);
            this.Picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.Picture.TabIndex = 0;
            this.Picture.TabStop = false;
            this.Picture.Click += new System.EventHandler(this.Picture_Click);
            // 
            // ToolBox
            // 
            this.BackColor = System.Drawing.Color.WhiteSmoke;
            this.Controls.Add(this.Picture);
            this.Controls.Add(this.Pioneza);
            this.Controls.Add(this.CloseButton);
            this.Controls.Add(this.Title);
            this.Controls.Add(this.splitter1);
            this.Controls.Add(this.panel);
            this.Name = "ToolBox";
            this.Size = new System.Drawing.Size(216, 456);
            this.Load += new System.EventHandler(this.ToolBox_Load);
            ((System.ComponentModel.ISupportInitialize)(this.Picture)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		private ListView VisibleLV = new ListView();

		public void AddTab(string Caption,int ImgIndex,ImageList ImgList)
		{
			ToolBoxTab tab = new ToolBoxTab();
			tab.Caption = Caption;
			tab.ImgIndex = ImgIndex;
			tab.ImgList = ImgList;
			AddTab(tab);
		}
		public void AddTab(ToolBoxTab Tab)
		{
			int nr = arrTabs.Count;

			if(ClientRectangle.Height<=(nr+5)*(ButtonHeight)+YOffset)
			{
				MessageBox.Show("You cannot add more tabs because it will fill up the list space");
				return;
			}

			Tab.button.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			//Tab.button.ImageAlign = System.Drawing.ContentAlignment.BottomLeft;
			if(VisibleLV == null)
			{
				Tab.button.Location = new System.Drawing.Point(XOffset,(ButtonHeight+2)*nr+YOffset);
			}
			else
			{
				Tab.button.Location = new System.Drawing.Point(XOffset,VisibleLV.Location.Y+VisibleLV.Height);
			}
			Image image1 = Image.FromFile("ListBoxButton.BMP");
			//Tab.button.Size = new Size(ButtonWidth, ButtonHeight);
			Tab.button.Size = new Size(image1.Width, image1.Height);
			Tab.button.Name = "button";
			//Tab.button.ImageList = Tab.ImgList;
			//Tab.button.ImageIndex = Tab.ImgIndex;
			Tab.button.Image = image1;
			Tab.button.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			Tab.button.TabIndex = 0;
			Tab.button.Text = Tab.Caption;
			Tab.button.Show();
			Tab.button.Click += new System.EventHandler(this.Tab_Click);

			
			ListView lv = Tab.ListV ;

			lv.Columns.Add(Tab.Caption,ButtonWidth-20,HorizontalAlignment.Left);
			lv.FullRowSelect = true;
			lv.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;

			lv.ContextMenu = CMenu;
			lv.LabelEdit = true;
			lv.MultiSelect = false;
			lv.View = System.Windows.Forms.View.Details;
			lv.BorderStyle = System.Windows.Forms.BorderStyle.None/*FixedSingle*/;
			

			lv.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Mouse_Down);
			lv.Click += new System.EventHandler(this.List_Click);

			Point pt = new Point(XOffset,(nr+1)*(ButtonHeight+2)+YOffset);
			Size sz = new Size(ListWidth,ClientRectangle.Height-(ButtonHeight+2)*(nr+2)-YOffset);;

			ScrollButton1.Location = new Point(ScrollButtonX,pt.Y-(ButtonHeight+2));
			ScrollButton2.Location = new Point(ScrollButtonX,pt.Y+sz.Height);


			lv.Location = new Point(XOffset,Tab.button.Location.Y+ButtonHeight+2);
			lv.Size = new Size(ListWidth,0);

			EndAllMovement();

			SizeObject so = new SizeObject(lv,StartFaze,EndFaze,sz,pt,true,Acceleration,Bounce);
			arrSizeList.Add(so);

			if(VisibleLV!=null)
			{
				SizeObject so2 = new SizeObject(VisibleLV,StartFaze,EndFaze,new Size(ButtonWidth,0),VisibleLV.Location,false,Acceleration,Bounce);
				arrSizeList.Add(so2);
			}


			lv.SmallImageList = Tab.ImgList;
			this.Controls.Add(Tab.button);
			this.Controls.Add(lv);
			ScrollButton1.BringToFront();
			ScrollButton2.BringToFront();
			arrTabs.Add(Tab);

			SetVisibleLV(lv);

			int Index = arrTabs.Count-1;
			for(int i=0;i<=Index;i++)
			{
				MoveObject mo = new MoveObject((Control)GetTab(i).button,StartFaze,EndFaze,new Point(XOffset,(ButtonHeight+2)*i+YOffset),Acceleration,Bounce);
				arrMoveList.Add(mo);
			}
			ResizeButtons(arrTabs.Count-1,-1);

		}
		private ArrayList arrTabs = new ArrayList();
		
		public ToolBoxTab GetTab(int Index)
		{
			if(Index>=arrTabs.Count||Index<0)
			{
				MessageBox.Show("Invalid Tab index at GetTab:"+Index.ToString());
				return null;
			}
			return ((ToolBoxTab)arrTabs[Index]);
		}

		private ArrayList arrMoveList = new ArrayList();
		private ArrayList arrSizeList = new ArrayList();
		
		public void AddItem(string Caption, int ImageIndex, int TabIndex)
		{
			ToolBoxItem itm = new ToolBoxItem();
			itm.Caption = Caption;
			itm.ImageIndex = ImageIndex;
			AddItem(itm, TabIndex);
		}
		public void AddItem(ToolBoxItem Item, int TabIndex)
		{
			if(TabIndex>arrTabs.Count-1 && TabIndex<0)
			{
				MessageBox.Show("Invalid Index value given to AddItem");
				return;
			}
			GetTab(TabIndex).AddItem(Item);
			GetTab(TabIndex).ListV.Items.Add(Item.Caption,Item.ImageIndex);
			//GetTab(TabIndex).ListV.Items[0].Selected = true;
		}


		private int EndFaze = 20;
		private int StartFaze = 0;
		private void Tab_Click(object sender, System.EventArgs e)
		{
			if(!DoneMove) return;
            Point pt = ((Button)sender).Location;
			if(pt.Y == VisibleLV.Location.Y - (ButtonHeight+2)) return;
			if(pt.Y<VisibleLV.Location.Y)
			{
				int Index = (pt.Y+1-YOffset)/(ButtonHeight+2);
				int bottom = ClientRectangle.Bottom;
				int i = 0;
				for (i=arrTabs.Count-1;i>Index;i--)
				{
					MoveObject mo = new MoveObject((Control)GetTab(i).button,StartFaze,EndFaze,new Point(XOffset,bottom - (arrTabs.Count-i)*(ButtonHeight+2)),Acceleration,Bounce);
					arrMoveList.Add(mo);
				}

				int OwnButtonIndex = (VisibleLV.Location.Y - (ButtonHeight+2)-YOffset)/(ButtonHeight+2);
				OwnButtonIndex++;
				VisibleLV.BringToFront();
                SizeObject so = new SizeObject(VisibleLV,StartFaze,EndFaze,new Size(ListWidth,0),new Point(XOffset,bottom - (arrTabs.Count-OwnButtonIndex)*(ButtonHeight+2)),false,Acceleration,Bounce);
				arrSizeList.Add(so);
			
				GetTab(Index).ListV.Show();
				SetVisibleLV(GetTab(Index).ListV);
				
				Size sz = new Size(ListWidth,bottom-(ButtonHeight+2)*(arrTabs.Count)-YOffset);
				VisibleLV.Size = new Size(ListWidth,0);

				SizeObject so2 = new SizeObject(VisibleLV,StartFaze,EndFaze,sz,VisibleLV.Location,true,Acceleration,Bounce);
				arrSizeList.Add(so2);
				
				VisibleLV.SendToBack();

				if(Index+1>=arrTabs.Count)
				{
					ResizeButtons(Index,-1);
				}
				else
				{
					ResizeButtons(Index,Index+1);
				}
				Point  spt = ScrollButton1.Location;
				spt.Y = GetTab(Index).button.Location.Y;
				ScrollButton1.Location = spt;

                ScrollButton2.Location = new Point(ScrollButtonX,GetTab(Index).button.Location.Y+(ButtonHeight+2));
				MoveObject mo2 = new MoveObject((Control)ScrollButton2,StartFaze,EndFaze,new Point(ScrollButtonX,VisibleLV.Location.Y + sz.Height),Acceleration,Bounce);
				arrMoveList.Add(mo2);

				if(OnTBTabChanged!=null)
					OnTBTabChanged(Index);
			}
			else
			{
				int Index = (pt.Y-VisibleLV.Size.Height+2-YOffset)/(ButtonHeight+2);

				for(int i=0;i<=Index;i++)
				{
					MoveObject mo = new MoveObject((Control)GetTab(i).button,StartFaze,EndFaze,new Point(XOffset,(ButtonHeight+2)*i+YOffset),Acceleration,Bounce);
					arrMoveList.Add(mo);
				}

                int bottom = ClientRectangle.Bottom;
				SizeObject so2 = new SizeObject(VisibleLV,StartFaze,EndFaze,new Size(ListWidth,0),VisibleLV.Location,false,Acceleration,Bounce);
				arrSizeList.Add(so2);

				GetTab(Index).ListV.Show();
				SetVisibleLV(GetTab(Index).ListV);
				ListView lv = VisibleLV;

				ResizeLists(-1);
				if (Index<arrTabs.Count-1)
				{
					ResizeButtons(Index,Index+1);
				}
				else
				{
					ResizeLists(Index);
					ResizeButtons(Index,-1);
				}
				ScrollButton1.Location = new Point(ScrollButtonX,GetTab(Index).button.Location.Y);
				MoveObject mobj1 = new MoveObject((Control)ScrollButton1,StartFaze,EndFaze,new Point(ScrollButtonX,lv.Location.Y - (ButtonHeight+2)),Acceleration,Bounce);
				arrMoveList.Add(mobj1);

				ScrollButton2.Location = new Point(ScrollButtonX,VisibleLV.Location.Y+VisibleLV.Size.Height);


				Size sz = lv.Size;
				Point pnt = lv.Location;
	
				lv.Size = new Size(ListWidth,0);
				Point buttpoint =new Point(XOffset,GetTab(Index).button.Location.Y);
				lv.Location = new Point(XOffset,buttpoint.Y+ButtonHeight+2);

                SizeObject so = new SizeObject(GetTab(Index).ListV,StartFaze,EndFaze,sz,pnt,true,Acceleration,Bounce);
				arrSizeList.Add(so);
				
				if(OnTBTabChanged!=null)
				OnTBTabChanged(Index);
			}
		}

		private void SetVisibleLV(ListView lv)
		{
			VisibleLV = lv;
			splitter1.BringToFront();
		}

		private void ResizeButtons(int i1, int i2)
		{
			for(int i=0;i<arrTabs.Count;i++)
			{
				Point pt = ScrollButton1.Location;
				pt.X = ScrollButtonX;
				ScrollButton1.Location = pt;

				pt = ScrollButton2.Location;
				pt.X = ScrollButtonX;
				ScrollButton2.Location = pt;

				if((i==i1)||(i==i2))
				{
					Size sz = GetTab(i).button.Size;
					sz.Width = ButtonWidth-19;
					GetTab(i).button.Size = sz;
				}
				else
				{
					Size sz = GetTab(i).button.Size;
					sz.Width = ButtonWidth;
					GetTab(i).button.Size = sz;
				}
			}
		}

		private void ResizeLists(int i)
		{
			for(int j=0;j<arrTabs.Count;j++)
			{
				Size sz = new Size(ListWidth,ClientRectangle.Height - (ButtonHeight+2)*arrTabs.Count - YOffset);
				GetTab(j).ListV.Size = sz;
				if(j==i)
				{
					sz = GetTab(j).ListV.Size;
					sz.Height =  sz.Height - (ButtonHeight+2);
					GetTab(j).ListV.Size = sz;
				}
			}
		}

		private int delay = 0;
		private int sbdelay = 0;
		private void TimerTick(object sender, System.EventArgs e)
		{
			Point pt =  this.PointToClient(MousePosition);
			Rectangle rect = new Rectangle(pt,new Size(1,1));
			bool MouseOver = (ClientRectangle.IntersectsWith(rect)&& !panel.ClientRectangle.IntersectsWith(rect));

			if (MouseOver) ExpandCommand = true;			
			
			if(arrMoveList.Count>0)
			{
				DoneMove = false;
			}
			else
			{
				DoneMove = true;
			}
			for (int i=0;i<arrMoveList.Count;i++)
			{
				MoveObject mo =((MoveObject)arrMoveList[i]);
				mo.Increment_State();
				if(mo.faze==mo.endfaze)
				{
					arrMoveList.RemoveAt(i);
					i--;
				}
			}
			for (int i=0;i<arrSizeList.Count;i++)
			{
				SizeObject so =((SizeObject)arrSizeList[i]);
				so.Increment_State();
				if(so.faze==so.endfaze)
				{
					arrSizeList.RemoveAt(i);
					i--;
				}
			}
			ScrollButton1.BringToFront();
			ScrollButton2.BringToFront();

			if((RetreatCommand)||(!MouseOver&& (delay+=(timer1.Interval)<20?20:timer1.Interval)>Delay&&Retreat))
			{
				ExpandCommand = false; 
				Size sz = this.Size;
				if (((sz.Width > XOffset)))
				{
					sz.Width -= 10;
					this.Size = sz;
					ChangeState((int)States.Compressing);
				}
				else
				{
					sz.Width = XOffset;
					this.splitter1.Hide();
					Size = sz;
					delay = 0;
					ChangeState((int)States.Compressed);
					RetreatCommand = false;
				}
			}
			if((ExpandCommand)||(MouseOver&& (delay+=20)>10))
			{
				Size sz = this.Size;
				if (sz.Width < ButtonWidth+XOffset+RightMargin)
				{
					sz.Width += 20;
					this.Size = sz;
					ChangeState((int)States.Extending);
				}
				else
				{
					sz.Width = XOffset + ButtonWidth+2+RightMargin;
					this.splitter1.Show();
					Size = sz;
					delay = 0;
					ChangeState((int)States.Extended);
					ExpandCommand = false;
				}
			}
			if(SB1Down && (sbdelay+=(timer1.Interval<20)?20:timer1.Interval)>300)
			{
				ScrollUp();
				sbdelay-=20;
			}			
			if(SB2Down && (sbdelay+=(timer1.Interval<20)?20:timer1.Interval)>300)
			{
				ScrollDown();
				sbdelay-=20;
			}
		}
		private void Mouse_Down(object sender, System.Windows.Forms.MouseEventArgs e)
		{
		    base.OnMouseDown(e);

			ListView lv =((ListView)sender);
			ListViewItem itm = lv.GetItemAt(e.X,e.Y);
			if(itm==null) return;
		
			List_Click(sender,e);
			lv.DoDragDrop(lv.Columns[0].Text + Separator + lv.SelectedItems[0].Text, DragDropEffects.Copy);
		}

		public bool SetSize(Size sz)
		{
		
			bool statewidth = ((state==1)&&(sz.Width!=Width))||(state!=1);
			if (state==1) 
			{
				this.Size = new Size(Width,sz.Height);
			}
			else
			{
				this.Size = sz;
			}

			if((state!=1)&&((sz.Height<=(arrTabs.Count+5)*(ButtonHeight)+YOffset)||(sz.Width<XOffset+50)))
			{
				return false;
			}

			if(statewidth)
				ButtonWidth = sz.Width - XOffset-2-RightMargin;
			ListWidth = ButtonWidth+100;
			if(statewidth)ScrollButtonX = sz.Width - 19 - RightMargin;
			int bottom = ClientRectangle.Bottom;
			int Index = (VisibleLV.Location.Y - ButtonHeight+2 +1 - YOffset)/(ButtonHeight+2);

			if(Index<0)
			{
				
				ScrollButton2.Location = new Point(ScrollButtonX,YOffset+ButtonHeight+2);
				ScrollButton1.Location = new Point(ScrollButtonX,YOffset);
				return true;
			}

			if (Index<arrTabs.Count-1)
			{
				ResizeButtons(Index,Index+1);
			}
			else
			{
				ListView lv = GetTab(Index).ListV;
				lv.Size = new Size(ListWidth,ClientRectangle.Height-YOffset-(arrTabs.Count+1)*(ButtonHeight+2));
				ResizeButtons(Index,-1);
			}

			ScrollButton2.Location = new Point(ScrollButtonX,GetTab(Index).ListV.Location.Y+GetTab(Index).ListV.Size.Height);
			ScrollButton1.Location = new Point(ScrollButtonX,GetTab(Index).button.Location.Y);
			
			int i = 0;
			for (i=arrTabs.Count-1;i>Index;i--)
			{
				((ToolBoxTab)arrTabs[i]).button.Location = new Point(XOffset,bottom - (arrTabs.Count-i)*(ButtonHeight+2));
			}

			for(i=0;i<arrTabs.Count;i++)
			{
				GetTab(i).ListV.Columns[0].Width = ButtonWidth - 20;
			}

			return true;

		}


		private void CloseButton_Click(object sender, System.EventArgs e)
		{
			Hide();
		}

		private void Pioneza_Click(object sender, System.EventArgs e)
		{
			if(Retreat)
			{
				Retreat = false;
				this.Pioneza.Image = this.ButtonImageList.Images[1];
				Point pt = this.Location;
				pt.X-=XOffset;
				Location = pt;
			}
			else
			{
				Retreat = true;
				this.Pioneza.Image = this.ButtonImageList.Images[0];
				Point pt = this.Location;
				pt.X+=XOffset;
				Location = pt;
			}
		}

		public bool RemoveTab(int Index)
		{
			if(Index>=arrTabs.Count||arrTabs.Count==0||Index<0)
			{
				MessageBox.Show("Index out of range at RemoveTab:"+Index.ToString());
				return false;
			}

			EndAllMovement();

			if(Index == arrTabs.Count-1 && Index!=0)
			{
				this.Controls.Remove(GetTab(Index).button);
				this.Controls.Remove(GetTab(Index).ListV);
				arrTabs.RemoveAt(Index);
				Tab_Click(GetTab(Index-1).button,new EventArgs());
			}
			else
			{	
				if(Index == 0 && arrTabs.Count==1)
				{
						this.Controls.Remove(GetTab(Index).button);
						this.Controls.Remove(GetTab(Index).ListV);
						arrTabs.RemoveAt(Index);
				}
				else
				{
					this.Controls.Remove(GetTab(Index).button);
					this.Controls.Remove(GetTab(Index).ListV);
					arrTabs.RemoveAt(Index);

					VisibleLV.Hide();

					int i =0;
					for(i=Index;i<arrTabs.Count;i++)
					{
						Point pt = GetTab(i).button.Location;
						pt.Y = (ButtonHeight+2)*i + YOffset;
						GetTab(i).button.Location = pt ;

						pt = GetTab(i).ListV.Location;
						pt.Y = (ButtonHeight+2)*(i+1) + YOffset;
						GetTab(i).ListV.Location = pt ;
					}
					i--;
					SetVisibleLV(GetTab(i).ListV);
					VisibleLV.Show();
					ResizeButtons(i,-1);
					ResizeLists(i);

					Size sz = VisibleLV.Size;
					VisibleLV.Size = new Size(ListWidth,0);

					SizeObject so = new SizeObject(VisibleLV,StartFaze,EndFaze,sz,VisibleLV.Location,true,Acceleration,Bounce);
					arrSizeList.Add(so);

					ScrollButton1.Location = new Point(ClientRectangle.Width-19,VisibleLV.Location.Y-(ButtonHeight+2));
					ScrollButton2.Location = new Point(ClientRectangle.Width-19,VisibleLV.Location.Y+sz.Height);
				}
			}
			return true;
		}

		public void EndAllMovement()
		{
			for(int i=0;i<arrSizeList.Count;i++)
			{
				((SizeObject)arrSizeList[i]).faze = EndFaze-1;
				((SizeObject)arrSizeList[i]).Increment_State();
				arrSizeList.RemoveAt(i);
				i--;
			}
		
			for(int i=0;i<arrMoveList.Count;i++)
			{
				((MoveObject)arrMoveList[i]).faze = EndFaze-1;
				((MoveObject)arrMoveList[i]).Increment_State();
				arrMoveList.RemoveAt(i);
				i--;
			}

		}

		public int GetTabCount()
		{
			return arrTabs.Count;
		}

		public int SelectedTab
		{
			get
			{
				return (int)((VisibleLV.Location.Y-(ButtonHeight+2)+1-YOffset)/(ButtonHeight+2));
			}
			set
			{
				int sel = value;
				if(sel>=arrTabs.Count||sel<0)
				{
					MessageBox.Show("Invalid Index at SelectedTab:"+sel.ToString());
					return;
				}	
				Tab_Click(GetTab(sel).button,new EventArgs());
			}
		}

		public bool RemoveItem(int TabIndex, int ItemIndex)
		{
			if(TabIndex>=arrTabs.Count||TabIndex<0)
			{
				MessageBox.Show("Invalid TabIndex at RemoveItem:"+TabIndex.ToString());
				return false;
			}
			if(ItemIndex>=GetTab(TabIndex).ListV.Items.Count ||ItemIndex<0)
			{
				MessageBox.Show("Invalid ItemIndex at RemoveItem:"+ItemIndex.ToString());
				return false;
			}
			GetTab(TabIndex).ListV.Items.RemoveAt(ItemIndex);
			return true;
		}

		private void Splitter_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			this.splitter1.Capture = true;
		}

		private void Splitter_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(e.Button == MouseButtons.Left)
			{
				Size sz = this.Size;

					sz.Width = e.X+ClientRectangle.Width;
			if(sz.Width>XOffset+50)
				{
					EndAllMovement();
					SetSize(sz);
					if(e.X>0)
					{
						Invalidate();
						this.Update();
					}
				}
			}
		}

		private void Splitter_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			int y = e.ClipRectangle.Bottom;
			int top = e.ClipRectangle.Top;

				e.Graphics.DrawLine(Pens.LightGray,new Point(0,top),new Point(0,y));
				e.Graphics.DrawLine(Pens.DarkGray,new Point(1,top),new Point(1,y));
				e.Graphics.DrawLine(Pens.Black,new Point(2,top),new Point(2,y));
				e.Graphics.DrawLine(Pens.Black,new Point(3,top),new Point(3,y));

		}

		private void Menu_Autohide_Click(object sender, System.EventArgs e)
		{
			Pioneza_Click(Pioneza,new EventArgs());
			if(Retreat)
				((MenuItem)sender).Checked = true;
			else
				((MenuItem)sender).Checked = false;

		}

		private void Menu_Hide_Click(object sender, System.EventArgs e)
		{
			Hide();
		}

		private void Menu_Dock_Left_Click(object sender, System.EventArgs e)
		{
			Dock = DockStyle.Left;
		}

		private void Menu_Dock_Right_Click(object sender, System.EventArgs e)
		{
			Dock = DockStyle.Right;
		}

		public int TabTime
		{
			get
			{
				return EndFaze*20;
			}
			set
			{
				EndFaze = (int)value/20;
			}
		}

		public int DelayBeforeRetreat
		{
			get
			{
				return Delay;
			}
			set
			{
				Delay = value;
			}
		}

		public int TabAcceleration
		{
			get
			{
				return Acceleration;
			}
			set
			{
				if(value>0&&value<11)
				Acceleration = value;
			}
		}

		public bool TabBounce
		{
			get
			{
				return Bounce;
			}
			set
			{
				Bounce = value;
			}
		}

		public int TimerInterval
		{
			get
			{
				return timer1.Interval;
			}
			set
			{
				timer1.Interval = value;
			}
		}

		public string TitleText
		{
			get
			{
				return Title.Text;
			}
			set
			{
				Title.Text = value;
			}
		}

		public override DockStyle Dock
		{
			get { return DockStyle.None; }

			set
			{
				 return;
			}

		}

		private void AdjustPositions(DockStyle ds)
		{
			return;
/*			Invalidate(true);
			Update();
			if((ds == DockStyle.Left&&XOffset<RightMargin)||(ds == DockStyle.Right&&XOffset>RightMargin))
			{
				int OldXOffset = XOffset;
				XOffset = RightMargin;
				RightMargin = OldXOffset;
			}
			if(ds == DockStyle.Left)
			{
				Picture.Location = new Point(0,0);
				this.splitter1.Dock = DockStyle.Right;
			}
			if(ds == DockStyle.Right)
			{
				Picture.Location = new Point(ClientRectangle.Width-Picture.Width,0);
				this.splitter1.Dock = DockStyle.Left;
			}
			

			ScrollButton1.Location = new Point(ClientRectangle.Width-19 -RightMargin,ScrollButton1.Location.Y);
			ScrollButton2.Location = new Point(ClientRectangle.Width-19 -RightMargin,ScrollButton2.Location.Y);
			Title.Location = new Point(XOffset+2,Title.Location.Y);
			Title.Width = ButtonWidth;

			Pioneza.Location = new Point(ClientRectangle.Width-RightMargin-2*Pioneza.Width-4,Pioneza.Location.Y);
			CloseButton.Location = new Point(ClientRectangle.Width-RightMargin-CloseButton.Width-2,CloseButton.Location.Y);

			for(int i=0;i<arrTabs.Count;i++)
			{
				GetTab(i).button.Location = new Point(XOffset,GetTab(i).button.Location.Y);
				GetTab(i).ListV.Location = new Point(XOffset,GetTab(i).ListV.Location.Y);
			}
*/		
		}

		private void Menu_About_Click(object sender, System.EventArgs e)
		{
			AboutListBoxDlg af = new AboutListBoxDlg();
			af.Show();
		}

		private void ToolBox_Load(object sender, System.EventArgs e)
		{
		
		}

		public Image ToolBoxImage
		{
			get
			{
				return Picture.Image;
			}
			set
			{
				Picture.Image = value;
			}
		}

		public void Compress()
		{
			ExpandCommand = false;
			RetreatCommand = true;
		}

		public void Expand()
		{
			ExpandCommand = true;
			RetreatCommand = false;
		}

		public string DragDropSeparatorText
		{
			get
			{
				return Separator;
			}
			set
			{
				Separator = value;
			}
		}

		public int State
		{
			get
			{
				return state;
			}
		}

		private void ChangeState(int CState)
		{
			if(state==CState) return;
			this.state = CState;
			if (OnTBStateChanged!=null)
			OnTBStateChanged(CState);
		}

		private bool SB1Down = false;
		private bool SB2Down = false;

		private void SB1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			SB1Down = true;
			ScrollUp();
		}

		private void SB1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			SB1Down = false;
			sbdelay = 0;
		}

		private void SB2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			SB2Down = true;
			ScrollDown();
		}

		private void SB2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			SB2Down = false;
			sbdelay = 0;
		}

		public bool ScrollUp()
		{
			if(VisibleLV.Items.Count==0) return false;
			ListViewItem itm = VisibleLV.GetItemAt(2,2);
			if(itm == null) return false;
			if(itm.Index==0) return false;
			VisibleLV.EnsureVisible(itm.Index-1);
			return true;
		}

		public bool ScrollDown()
		{
			if(VisibleLV.Items.Count==0) return false;
			ListViewItem itm = VisibleLV.GetItemAt(2,VisibleLV.Height-2);
			if(itm == null) return false;
			if(itm.Index==VisibleLV.Items.Count-1) return false;
			VisibleLV.EnsureVisible(itm.Index+1);
			return true;
		}

		private void Picture_Click(object sender, System.EventArgs e)
		{
			ExpandCommand = true;
		}

	}
}

